package com.jia.lidao.ui.message;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;


import com.jia.lidao.R;
import com.jia.lidao.UserUtils;
import com.jia.lidao.enity.Message;
import com.jia.lidao.enity.MessagetBeen;
import com.jia.lidao.network.http.BusinessManager;
import com.jia.lidao.network.http.MyCallback;
import com.jia.lidao.network.http.request.impl.MessagesRequest;
import com.jia.lidao.widget.CommonTitleBar;

import java.util.ArrayList;
import java.util.List;

import me.leefeng.lfrecyclerview.LFRecyclerView;

/**
 * Created by 00029935 on 2017/11/17.
 */
public class MessageOneFragment extends BaseFragment {
	private LFRecyclerView recyclerView;
	private ProgressBar loading;

	private MessageOneAdapter adapter;
	private List<MessagetBeen.MessagesBean.DataBean> messageOnes;
	private Message.CategoryBean data;
	private int page = 1;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		data = (Message.CategoryBean) getActivity().getIntent()
				.getSerializableExtra("object");
	}

	@Override
	protected int getInflateLayout() {
		return R.layout.common_fragment_layout2;
	}

	@Override
	protected void setUpView(View view) {
		recyclerView = (LFRecyclerView) view.findViewById(R.id.recyclerView);
		loading = (ProgressBar) view.findViewById(R.id.loading);

		CommonTitleBar commonTitleBar = (CommonTitleBar) view
				.findViewById(R.id.common_title_bar);
		commonTitleBar.setTitle(data.getName());

		adapter = new MessageOneAdapter();
		messageOnes = new ArrayList<>();
		adapter.setData(messageOnes);

		recyclerView.setAdapter(adapter);
		recyclerView.setLoadMore(true);
		recyclerView.setLFRecyclerViewListener(
				new LFRecyclerView.LFRecyclerViewListener() {
					@Override
					public void onRefresh() {
						queryData(true);
					}

					@Override
					public void onLoadMore() {
						queryData(false);
					}
				});
		queryData(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = super.onCreateView(inflater, container,
				savedInstanceState);
		return rootView;
	}

	private void queryData(final Boolean isReflush) {

		if (isFirstIn) {
			isFirstIn = false;
			loading.setVisibility(View.VISIBLE);
		}

		if (isReflush) {
			page = 1;
			messageOnes.clear();
		}

		BusinessManager.getInstance().getMessages(
				new MessagesRequest(page, 10, data.getMessage_category_id()),
				new MyCallback<MessagetBeen>() {
					@Override
					public void onSuccess(MessagetBeen result, String message) {
						if (result == null) {
							return;
						}
						List<MessagetBeen.MessagesBean.DataBean> DataBeans = result
								.getMessages().getData();
						if (null != DataBeans && !DataBeans.isEmpty()) {
							messageOnes.addAll(DataBeans);
							++page;
						}
						List<MessagetBeen.MessagesBean.DataBean> list = new ArrayList<>();
						list.addAll(messageOnes);
						adapter.reflashData(list);
					}

					@Override
					public void onError(String errorCode, String message) {
						showMsgToast(message);
					}

					@Override
					public void onFinished() {
						if (null != loading) {
							loading.setVisibility(View.GONE);
						}

						if (null != recyclerView) {
							recyclerView.stopLoadMore();
							recyclerView.stopRefresh(true);
						}
					}

					@Override
					public void onRefreshToken() {
						UserUtils.getInstance().goToLogin(getActivity());
					}
				});
	}
}
